import React, { useState, useEffect } from 'react';
import { useHistory, Link } from "react-router-dom";
import { Toast, Modal,ActivityIndicator } from 'antd-mobile';
import './home.less'
import Blank from "../../../components/blank/Blank";
import avatar from '../../../libs/img/user/default-avatar.png'
// import user_register from '../../../libs/img/user/user-register2-icon.png'
// import user_query from '../../../libs/img/user/user-query-icon.png'  

// import user_return from '../../../libs/img/user/user-return-icon.png'
// import user_money from '../../../libs/img/user/user-money-icon.png'
// import user_seas from '../../../libs/img/user/user-seas-icon.png'
// import user_discount from '../../../libs/img/user/user-discount-icon.png'
// import user_target from '../../../libs/img/user/user-target-icon.png'
import http from '../../../model/http'
import code from "../../../model/code";

import user_query from '../../../libs/img/user/homeColor1.png'
import user_target from '../../../libs/img/user/homeColor2.png'
import user_yuyue from '../../../libs/img/user/homeColor3.png'
import user_register from '../../../libs/img/user/homeNoColor4.png'
import user_return from '../../../libs/img/user/homeNoColor5.png'
import user_return_color from '../../../libs/img/user/homeColor5.png'
import user_money from '../../../libs/img/user/homeNoColor6.png'
import user_seas from '../../../libs/img/user/homeNoColor7.png'
import user_discount from '../../../libs/img/user/homeNoColor8.png'
import user_schedule from '../../../libs/img/user/homeColor9.png'
import user_electron from '../../../libs/img/user/homeColor10.png'
import user_billing from '../../../libs/img/user/homeColor11.png'
import user_clearLocalstorage from '../../../libs/img/user/homeColor12.png'
import user_doctor from '../../../libs/img/user/homeColor13.png'
import gift_write_off from '../../../libs/img/user/homeColor14.png'
import parking_management from '../../../libs/img/user/homeColor15.png'
import log from '../../../libs/img/user/log.png'
import sound from '../../../libs/img/user/sound.png'
import zxjl from '../../../libs/img/user/zxjl.png'


import Vconsole from 'vconsole'







function Home() {
  const history = useHistory();
  const [userRole, setUserRole] = useState({})
  const [userDetail, setUserDetail] = useState({})
  const user = JSON.parse(window.localStorage.getItem('user'))
  const check = JSON.parse(window.localStorage.getItem('check'))

  const [jurisdiction, setJurisdiction] = useState('')
  const [doctorDetail,setDoctorDetail] = useState('')
  const [animatingShow,setAnimatingShow] = useState(false)

  const [testUser,setTestUser]=useState({})

  const [hosId,setHosId] =useState()

  const getUserRole = async () => {
    let check = await code.checkBind()
    setHosId(check.hosId)
    let getContext = await code.getContext()
    const res = await http.post('/wxapisg/getUserRole', {
      hosId: check.hosId,
      userCrmId: check.userCrmId
    });
    console.log("getUserRole", res)
    if (res.code == 0) {
      setUserRole(res)
      /*此内容为模拟科室主任数据---开始,正式需注释*/
      // res.hosId=5
      // res.usertype=2
      // res.userId=1409401426828189697
      // res.deptId = 10
      /*此内容为模拟数据---结束*/
      window.localStorage.setItem('userRole', JSON.stringify(res))


      setTestUser(JSON.parse(window.localStorage.getItem('user')))

    

      if(JSON.parse(window.localStorage.getItem('user')).userid == "XiongLiDi"){
        const vConsole = new Vconsole()
      }
     




    }

    // 用户详细信息
    const userDetails = await http.post('/wxapisg/getUserInfo', {
      hosId: check.hosId,
    })

    // 医生的详情
    const doctorDetail = await http.post('/expert/index')
    setDoctorDetail(doctorDetail)

    if (userDetails.code == 0) {
      setUserDetail(userDetails)
    }

    // 工作台新加权限
    getJurisdiction()

    setAnimatingShow(false)
  }

  useEffect(() => {

    setAnimatingShow(true)
    getUserRole()  
  }, [])

  // 工作台初始化权限
  const getJurisdiction = async () => {
    const result = await http.post('/manager/userInfoInit')
    if (result.code == 0) {
      setJurisdiction(result)
      window.sessionStorage.setItem('doctorJurisdiction', JSON.stringify(result))
    } else {
      Toast.fail(result.msg)
    }







  }



  const toTarget = () => {
    const userRole = JSON.parse(window.localStorage.getItem('userRole'))
    if (userRole.usertype == 1 || userRole.usertype == 2) {

      history.push({
        pathname: '/target',
        query: {
          usertype: userRole.usertype,
          deptId: userRole.deptId
        }
      })
    } else {
      Toast.fail('无权限查看')
      return;
    }
  }

  // 新增
  const add = () => {
    history.push("/user/edit?type=add&id=0");
  }

  // 在线预约
  const toBooking = () => {
    history.push({
      pathname: '/yuyue/list',
    })
  }
  const showhtml = (htmlString) => {
    var html = { __html: htmlString };
    return <div dangerouslySetInnerHTML={html}></div>;
  }

  const userlistGo = (flag) => {
    history.push(`/user/userHomeList`, {
      flag: flag
    })
  }

  // 日程管理
  const toSchedule = () => {
    history.push(`/scheduleNav`, {})
  }

  // 医疗电子化
  const toElectron = () => {
    history.push(`/medicalElectronNav`, {})
  }

  // 在线开单
  const toBilling = () => {
    history.push(`/user`, {
      gotoType: 'billing'
    })
  }

  // 医生工作站
  const toDoctorWorkstation = () => {
    history.push(`/doctorWorkstation`)
  }

  // 清空缓存
  const removeStroage = () => {
    Modal.alert('提示', '确认清除缓存', [
      { text: '取消' },
      {
        text: '确认', onPress: async () => {
          window.localStorage.clear()
          window.sessionStorage.clear()
          Toast.success('清除缓存成功')
          setTimeout(() => {
            window.close()
            // history.goBack(-1)
          }, 1000);

        }
      },
    ])

  }
  //咨询记录
  const jumpZxjl=()=>{
      // Toast.info("即将上线!")
      history.push('/consultationRecords')

  }
  
  // 客户回访
  const toClientReview = () => {
    let authority = Number(jurisdiction.authority)
    // let authority =2 
    
    if(authority == 2 || authority ==9){
      history.push('/clientReview/soundRecord')
    }else{
      history.push('/clientReview')
    } 
  }


  //客户盘点
  const toCustomer =()=>{
      //  Toast.info("即将上线!")
      history.push('/customerInventory')


  }

  // 回访录音
  const returnVisitRecording = ()=>{

    history.push('/clientReview/soundList')

    
  }

  // 礼品核销 
  const toGiftWriteOff = () => {
    history.push('/giftWriteOff')
  }
  const parkingManagement = () => {
    history.push('/parkingInfo')
  }

  const operationLog = () => {
    history.push('/operationLog')
  }
  //绑定子账号
  const newBind = () =>{
    history.push('/acountList',{
      
    })

  }

  // 测试
  const jumpTest = ()=>{
    // userlistGo("4")
    // let userid = JSON.parse(localStorage.getItem("user")).userid
    // /yuyue/project?outUserId=

    // history.push('/acountList',{
      
    // })



  }


  return (
    <div>
      {
        check?.isBind !== 1 ? (
          <Blank />
        ) : (
          <div className='home'>
            <div className='home-top g-flex-col' style={jurisdiction.authority == '3'?{height:'4.7rem'}:{}}>
              <div className='info-wrapper  g-flex g-y-center'>
                {console.log(user)}
                <img src={user.thumb_avatar} alt="" />
                <div className='info g-ml15 g-flex-col g-f14 '>
                  <div className='g-mb20 g-let-spa-3 g-f30 g-c-FFF'><b>{userRole.name}</b></div>
                  <div className='g-f26' style={{ color: '#ededed' }}>{userRole.position}</div>
                </div>
              </div>
              {[1,2,4,5,7,9].indexOf(Number(jurisdiction.authority)) != -1 && (
                <div className='home-target-wrapper g-flex g-flex-wrap g-x-between'>
                  <div className='target-item g-flex-col g-x-center' onClick={() => userlistGo('1')}>
                    <div className='target-item-number'>{userDetail.customer_count}</div>
                    <div className='g-f24' style={{ color: '#ededed' }}>我的客户</div>
                  </div>
                  <div className='target-item g-flex-col g-x-center' onClick={() => userlistGo('2')}>
                    <div className='target-item-number'>{userDetail.zixuncount}</div>
                    <div className='g-f24' style={{ color: '#ededed' }}>今日咨询</div>
                  </div>
                  <div className='target-item g-flex-col g-x-center' onClick={() => userlistGo('3')}>
                    <div className='target-item-number'>{userDetail.chengjiaocount != '' ? userDetail.chengjiaocount : 0}</div>
                    <div className='g-f24' style={{ color: '#ededed' }}>今日成交</div>
                  </div>
                  {/* <div className='target-item g-flex-col g-x-center'>
                  <div className='target-item-number'>{showhtml(userDetail.goal)}</div>
                  <div className='g-f24' style={{ color: '#ededed' }}>目标业绩</div>
                </div> */}
                  <div className='target-item g-flex-col g-x-center'>
                    <div className='target-item-number'>{showhtml(userDetail.goal_baodi)}</div>
                    <div className='g-f24' style={{ color: '#ededed' }}>保底目标</div>
                  </div>
                  <div className='target-item g-flex-col g-x-center'>
                    <div className='target-item-number'>{showhtml(userDetail.goal_fendou)}</div>
                    <div className='g-f24' style={{ color: '#ededed' }}>奋斗目标</div>
                  </div>
                  <div className='target-item g-flex-col g-x-center'>
                    <div className='target-item-number'>{showhtml(userDetail.goal_chongci)}</div>
                    <div className='g-f24' style={{ color: '#ededed' }}>冲刺目标</div>
                  </div>

                  <div className='target-item g-flex-col g-x-center'>
                    <div className='target-item-number'>{userDetail.total}</div>
                    <div className='g-f24' style={{ color: '#ededed' }}>日营收金额</div>
                  </div>
                  <div className='target-item g-flex-col g-x-center'>
                    <div className='target-item-number'>{userDetail.real}</div>
                    <div className='g-f24' style={{ color: '#ededed' }}>日实收金额</div>
                  </div>
                  <div className='target-item g-flex-col g-x-center'>
                    <div className='target-item-number'>{userDetail.monthreal}</div>
                    <div className='g-f24' style={{ color: '#ededed' }}>月实收金额</div>
                  </div>
                </div>
              )}
              {[3,6,8].indexOf(Number(jurisdiction.authority)) != -1 && (
                <div className='home-target-wrapper g-flex g-flex-wrap g-x-between' style={{paddingTop: '.5rem'}}>
                  <div className='target-item g-flex-col g-x-center'>
                    <div className='target-item-number'>{doctorDetail.todayallcount}</div>
                    <div className='g-f24' style={{ color: '#ededed' }}>今日台数</div>
                  </div>
                  <div className='target-item g-flex-col g-x-center'>
                    <div className='target-item-number'>{doctorDetail.is_end_count}</div>
                    <div className='g-f24' style={{ color: '#ededed' }}>已做台数</div>
                  </div>
                  <div className='target-item g-flex-col g-x-center'>
                    <div className='target-item-number'>{doctorDetail.tomorrowallcount}</div>
                    <div className='g-f24' style={{ color: '#ededed' }}>明日台数</div>
                  </div>
                </div>
              )}

            </div>
            <div className='item-wrapper g-flex g-flex-wrap'>

              {[1,2,4,5,7,9].indexOf(Number(jurisdiction.authority)) != -1 && (
                <Link to="/user">
                  <div className='item g-flex-col g-flex-y-center'>
                    <img src={user_query} alt=''></img>
                    <div className='g-c-333 f14' style={{ marginTop: '-0.1rem' }}>客户查询</div>
                  </div>
                </Link>
              )}

             
              { ( [1,9].indexOf(Number(jurisdiction.authority)) > -1 && hosId == '1' )  ? (
                <div className='item g-flex-col g-flex-y-center' onClick={newBind.bind(this)}>
                  <img src={user_target} alt=''></img>
                  <div className='g-c-333 f14' style={{ marginTop: '-0.1rem' }}>新增助诊账号</div>
                </div>
              ):''}






        
              {[2,9].indexOf(Number(jurisdiction.authority)) != -1 ? (
                <div className='item g-flex-col g-flex-y-center' onClick={toTarget.bind(this)}>
                  <img src={user_target} alt=''></img>
                  <div className='g-c-333 f14' style={{ marginTop: '-0.1rem' }}>目标管理</div>
                </div>
              ):''}




              <div className='item g-flex-col g-flex-y-center' onClick={toBooking.bind(this)}>
                <img src={user_yuyue} alt=''></img>
                <div className='g-c-333 f14' style={{ marginTop: '-0.1rem' }}>在线预约</div>
              </div>
              <div className='item g-flex-col g-flex-y-center' onClick={toSchedule.bind(this)}>
                <img src={user_schedule} alt=''></img>
                <div className='g-c-333 f14' style={{ marginTop: '-0.1rem' }}>日程管理</div>
              </div>
              {[1,2,3,4,5,6,8,9].indexOf(Number(jurisdiction.authority)) != -1 && (
                <div className='item g-flex-col g-flex-y-center' onClick={toElectron.bind(this)}>
                  <img src={user_electron} alt=''></img>
                  <div className='g-c-333 f14' style={{ marginTop: '-0.1rem' }}>医疗电子化</div>
                </div>  
              )}
              {[1,2,3,4,5,6,8,9].indexOf(Number(jurisdiction.authority)) != -1 && (
                <div className='item g-flex-col g-flex-y-center' onClick={toBilling.bind(this)}>
                  <img src={user_billing} alt=''></img>
                  <div className='g-c-333 f14' style={{ marginTop: '-0.1rem' }}>在线开单</div>
                </div>
              )}
              

              
              
              
              {[1,2,3,5,6,8,9].indexOf(Number(jurisdiction.authority)) != -1 ? (<div className='item g-flex-col g-flex-y-center' onClick={toDoctorWorkstation.bind(this)}>
                <img src={user_doctor} alt=''></img>
                <div className='g-c-333 f14' style={{ marginTop: '-0.1rem' }}>医生工作站</div>
              </div>):''}

              {/* {[7,9].indexOf(Number(jurisdiction.authority)) != -1 && (
                <div className='item g-flex-col g-flex-y-center' onClick={toClientReview.bind(this)}>
                  <img src={user_return_color} alt=''></img>
                  <div className='g-c-333' style={{ marginTop: '-0.1rem' }}>客户回访</div>
                </div>
              )} */}
              <div className='item g-flex-col g-flex-y-center' onClick={toClientReview.bind(this)}>
                <img src={user_return_color} alt=''></img>
                <div className='g-c-333 f14' style={{ marginTop: '-0.1rem' }}>客户回访</div>
              </div>

              {[1,2,9].indexOf(Number(jurisdiction.authority)) != -1 ? 
              
              <div className='item g-flex-col g-flex-y-center' onClick={toCustomer.bind(this)} >
                <img src={user_return_color} alt=''></img>
                <div className='g-c-333 f14' style={{ marginTop: '-0.1rem' }}>客户盘点</div>
              </div>
              
              :''
              }


              
              {[2,9].indexOf(Number(jurisdiction.authority)) != -1 ? (<div className='item g-flex-col g-flex-y-center' onClick={returnVisitRecording.bind(this)}>
                <img src={sound} alt=''></img>
                <div className='g-c-333 f14' style={{ marginTop: '-0.1rem' }}>回访录音</div>
              </div>):''}
 

              { 
                 hosId == '1'? (
                    <div className='item g-flex-col g-flex-y-center' onClick={toGiftWriteOff.bind(this)}>
                      <img src={gift_write_off} alt=''></img>
                      <div className='g-c-333 f14' style={{ marginTop: '-0.1rem' }}>礼品核销</div>
                    </div>
                 ) :null
              
              }

             {
              hosId == '1'? (
                  <div className='item g-flex-col g-flex-y-center' onClick={parkingManagement.bind(this)}>
                    <img src={parking_management} alt=''></img>
                    <div className='g-c-333 f14' style={{ marginTop: '-0.1rem' }}>停车管理</div>
                  </div>
              ) :null
             }

            

              { [1,2,7,9].indexOf(Number(jurisdiction.authority)) != -1 ? 
                 <div className='item g-flex-col g-flex-y-center' onClick={() => {jumpZxjl()}}>
                 <img src={zxjl} alt=''></img>
                 <div className='g-c-333 f14' style={{ marginTop: '-0.1rem' }}>咨询记录</div>
               </div>
              :""}

            <div className='item g-flex-col g-flex-y-center' onClick={() => removeStroage()}>
                <img src={user_clearLocalstorage} alt=''></img>
                <div className='g-c-333 f14' style={{ marginTop: '-0.1rem' }}>清理缓存</div>
            </div>

            

              {[9].indexOf(Number(jurisdiction.authority)) != -1 ? (<div className='item g-flex-col g-flex-y-center' onClick={operationLog.bind(this)}>
                <img src={log} alt=''></img>
                <div className='g-c-333 f14' style={{ marginTop: '-0.1rem' }}>操作日志</div>
              </div>):''}

              <div className='item g-flex-col g-flex-y-center'>
                <img src={user_register} alt=''></img>
                <div className='g-c-999 f14' style={{ marginTop: '-0.1rem' }}>客户登记</div>
              </div>
             
              <div className='item g-flex-col g-flex-y-center'>
                <img src={user_money} alt=''></img>
                <div className='g-c-999 f14' style={{ marginTop: '-0.1rem' }}>预存金充值</div>
              </div>
              <div className='item g-flex-col g-flex-y-center'>
                <img src={user_seas} alt=''></img>
                <div className='g-c-999 f14' style={{ marginTop: '-0.1rem' }}>公海池</div>
              </div>
              <div className='item g-flex-col g-flex-y-center'>
                <img src={user_discount} alt=''></img>
                <div className='g-c-999 f14' style={{ marginTop: '-0.1rem' }}>折扣审核</div>
              </div>

              { 
                 (testUser.userid == "XiongLiDi" || testUser.userid == "LiHao")
                 ? (
                  <div className='item g-flex-col g-flex-y-center' onClick={()=>{jumpTest()}}>
                  <img src={user_discount} alt=''></img>
                  <div className='g-c-999 f14' style={{ marginTop: '-0.1rem' }}>测试{Number(jurisdiction.authority)} </div>
                </div>
                 ) : null 
              }

            
            </div>

           
            
            <div className='footer g-c-ccc g-text-center g-f26'>更多功能,敬请期待...</div>
          </div>
        )
      }
      <ActivityIndicator
        toast
        text="Loading..."
        animating={animatingShow}
      />
    </div>
    

  )
}

export default Home;


// customer_count 我的客户
// zixuncount 今日咨询
// chengjiaocount 今日成交
// goal 目标业绩
// total 营收金额
// real 实收金额


// 保底目标 
// 奋斗目标
// 冲刺目标


// 3 医生
// 8 护士
// 9 老板
// 1 咨询
// 2 科室主任
// 5 客服
// 4 咨询助理
// 6 美疗医生